﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;

namespace MetaDesigner
{
    /// <summary>
    /// Interaction logic for TermSetGroupPicker.xaml
    /// </summary>
    public partial class TermSetGroupPicker : Window
    {
        TermSet ts;

        /// <summary>
        /// Initializes a new instance of the <see cref="TermSetGroupPicker"/> class.
        /// </summary>
        /// <param name="ts">The ts.</param>
        public TermSetGroupPicker(TermSet ts)
        {
            InitializeComponent();
            this.ts = ts;

            foreach (Group g in SaveFile.scGroupsList)
            {
                ListBoxItem lbi = new ListBoxItem();
                lbi.Content = g.Name;
                lbi.Tag = g;
                if (ts.Group == g)
                {
                    lbi.IsSelected = true;
                }

                try
                {
                    SelectedGroup.Text = ts.Group.Name;
                }
                catch (Exception ex)
                {
                    Log.Append("Exception: The TermSet's group was null. " + ex.Message).AppendLine();
                }

                groupList.Items.Add(lbi);
            }
        }

        /// <summary>
        /// Handles the Click event of the New control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void New_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Group g = new Group(Prompt.Show("Enter the name of the new Group", "New Group"));
                SaveFile.scGroupsList.Add(g);
                ListBoxItem lbi = new ListBoxItem();
                lbi.Content = g.Name;
                lbi.Tag = g;
                groupList.Items.Add(lbi);

                ts.Group = g;
                SelectedGroup.Text = g.Name;
            }
            catch
            {

            }
        }

        /// <summary>
        /// Handles the Click event of the Select control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void Select_Click(object sender, RoutedEventArgs e)
        {
            Group selectedGroup = null;
            foreach (ListBoxItem lbi in groupList.Items)
            {
                if (lbi.IsSelected)
                {
                    selectedGroup = (Group)lbi.Tag;
                }
            }
            SelectedGroup.Text = selectedGroup.Name ?? "";
            ts.Group = selectedGroup;
        }

        /// <summary>
        /// Handles the Click event of the submitButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void submitButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the SelectionChanged event of the GroupsList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void GroupsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Group selectedGroup = null;
            foreach (ListBoxItem lbi in groupList.Items)
            {
                if (lbi.IsSelected)
                {
                    selectedGroup = (Group)lbi.Tag;
                }
            }

            if (selectedGroup != null && ts.Group != selectedGroup)
            {
                SelectBtn.IsEnabled = true;
            }
            else
            {
                SelectBtn.IsEnabled = false;
            }
        }
    }
}
